package com.kfh.ybracelet.location;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.base.HomeActivity;
import com.kfh.ybracelet.find.CaptureActivity;
import com.kfh.ybracelet.net.HttpUtil;
import com.kfh.ybracelet.utils.CommonUtils;
import com.kfh.ybracelet.utils.Constant;

import de.greenrobot.event.EventBus;


/**
 * 添加sn码
 * Created by Administrator on 2015/10/21.
 */
public class SnCodeActivity extends BaseActivity {

    private EditText et_sn;
    private TextView tv_done;
    private ImageView iv_left;
    private ImageView iv_delete_sn;
    private String name;
    //    private TextView tv_watch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_sn_code);
//        mApplication.addActivity(this);
        initView();
//OVENR9

        name = getIntent().getStringExtra("name");
    }

    private void initView() {

        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.input_imei));

        et_sn = (EditText) findViewById(R.id.et_sn);
        tv_done = (TextView) findViewById(R.id.tv_done);
//        tv_watch = (TextView) findViewById(R.id.tv_watch);
        iv_left = (ImageView) findViewById(R.id.iv_left);
        iv_delete_sn = (ImageView) findViewById(R.id.iv_delete_sn);
        ImageView iv_right = (ImageView) findViewById(R.id.iv_right);
        iv_right.setImageResource(R.mipmap.ic_scan);

        iv_right.setOnClickListener(this);
        iv_left.setOnClickListener(this);
        iv_delete_sn.setOnClickListener(this);
        tv_done.setOnClickListener(this);

//        et_sn.setText("OVENR9");
//        et_sn.setSelection(et_sn.getText().toString().length());

        et_sn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    //显示叉号
                    iv_delete_sn.setVisibility(et_sn.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);
                    et_sn.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                        }

                        @Override
                        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                            //显示叉号
                            iv_delete_sn.setVisibility(et_sn.getText().toString().trim().length() > 0 ? View.VISIBLE : View.INVISIBLE);
                        }

                        @Override
                        public void afterTextChanged(Editable editable) {

                        }
                    });

                } else {
                    iv_delete_sn.setVisibility(View.INVISIBLE);
                }
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mApplication.cancelPendingRequests(getClass().getName());
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.iv_left:
                finish();

                break;
            case R.id.iv_delete_sn:

                et_sn.setText("");

                break;
            case R.id.tv_done:

                if (CommonUtils.isNull(et_sn.getText().toString().trim())) {
                    showToast("请输入手表SN码!");
                    return;
                }

                getSnCodeData(Constant.ADDWATCH, et_sn.getText().toString().trim());

                break;

            case R.id.iv_right://TODO 扫一扫

                Intent mIntent = new Intent(this, CaptureActivity.class);
                startActivityForResult(mIntent, 0x101);

                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 0x101 && resultCode == 0x1002) {
            if (data != null) {
//                String imei = data.getStringExtra("imei");
                getSnCodeData(Constant.BINDYELLOW, data.getStringExtra("imei"));
            }
        }

    }

    private void getSnCodeData(String url, String imei) {

        JSONObject j = new JSONObject();

        j.put("id", sp.getString(Constant.CURRENTCHILDID, ""));
        j.put("token", sp.getString(Constant.TOKEN, ""));
        j.put("sn", imei);

//        String jsonString = JSONObject.toJSONString(snCodeReqBean);
        HttpUtil.setPostRequest("获取中...", this, Constant.WEB_ROOT + url,
                j.toJSONString(), getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

//                            org.json.JSONObject j = new org.json.JSONObject(str);
//                            if (j.getInt("code") == Constant.STATUS_SUCCESS) {

                            showToast("绑定成功");

                            //通知定位页面绑定了老人
                            EventBus.getDefault().post(true);

                            //通过sn绑定的是管理员权限
                            SharedPreferences.Editor edit = sp.edit();
//                            edit.putString(Constant.CURRENTCHILDID, datas.get(position).id);
                            edit.putString(Constant.CURRENTCHILDNAME, name);
                            edit.putInt(Constant.LEVEL, Constant.POWER_ADMIN);
                            edit.apply();

                            Intent mIntent = new Intent(SnCodeActivity.this, HomeActivity.class);
                            mIntent.putExtra("chooseLocation", true);
                            startActivity(mIntent);

//                            } else//弹出错误提示
//                                showToast(Constant.showErrorMsg(j.getInt("code"), SnCodeActivity.this));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                }, null);

    }
}
